﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace VehicleTest
{
    public partial class SerialSettings : Form
    {
        public MainWindows mainwindows;

        private string[] bandrate = { "9600", "14400", "19200", "38400", "56000", "57600", "115200", "128000" };
        private string[] parity = { "None", "Odd", "Even" };
        private string[] databits = { "5", "6", "7", "8" };
        private string[] stopbit = { "1", "2", "3" };
        private string[] datamode = { "Hex" };

        public SerialSettings(MainWindows mw)
        {
            InitializeComponent();

            mainwindows = mw;

            foreach (string s in SerialPort.GetPortNames())
            {
                comboBox_PortName.Items.Add(s);
            }
            foreach (string s in bandrate)
            {
                comboBox_BandRate.Items.Add(s);
            }
            foreach (string s in parity)
            {
                comboBox_Parity.Items.Add(s);
            }
            foreach (string s in databits)
            {
                comboBox_DataBits.Items.Add(s);
            }
            foreach (string s in stopbit)
            {
                comboBox_StopBit.Items.Add(s);
            }
            foreach (string s in datamode)
            {
                comboBox_DataMode.Items.Add(s);
            }

            comboBox_BandRate.Text = "38400";
            comboBox_DataBits.Text = "8";
            comboBox_Parity.Text = "None";
            comboBox_StopBit.Text = "1";
            comboBox_DataMode.Text = "Hex";

            if (comboBox_PortName.Items.Count > 0)
            {
                comboBox_PortName.SelectedIndex = 0;
                button_Sure.Enabled = true;
            }
            else
            {
                button_Sure.Enabled = false;
            }
        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }

        private void button_Sure_Click(object sender, EventArgs e)
        {
            mainwindows.PortName = comboBox_PortName.Text;
            mainwindows.BandRate = comboBox_BandRate.Text;
            mainwindows.DataBits = comboBox_DataBits.Text;
            mainwindows.Parity = comboBox_Parity.Text;
            mainwindows.StopBit = comboBox_StopBit.Text;
            mainwindows.DataMode = comboBox_DataMode.Text;

            mainwindows.OpenSerialOpen(true);
            this.Hide();
            this.Close();
        }

        private void comboBox_PortName_SelectedIndexChanged(object sender, EventArgs e)
        {
            button_Sure.Enabled = true;
        }

        private void comboBox_PortName_DropDown(object sender, EventArgs e)
        {
            comboBox_PortName.Items.Clear();
            foreach (string s in SerialPort.GetPortNames())
            {
                comboBox_PortName.Items.Add(s);
            }
        }

    }
}
